import {AfterViewInit, Component, OnDestroy, OnInit} from '@angular/core';
import {Message} from '../../../../../ng-tools/src/message/message';
import {Subscription} from 'rxjs';
import {MessageService, ProgressBarService} from '../../../../../ng-tools';
import {DvbConfigService} from '../../../../services/api/dvb-config.service';
import {MachineCodeService} from '../../../../services/api/machine-code.service';
import {FormBuilder, FormGroup, Validators} from '@angular/forms';

@Component({
  selector: 'register-code',
  templateUrl: './register-code.component.html',
  styleUrls: ['./register-code.component.scss']
})
export class RegisterCodeComponent implements OnInit, AfterViewInit, OnDestroy, Message {
  subscriptions: Subscription;
  registeForm: FormGroup;
  breadcrumb = [
    {name: '工具', cssIcon: 'fa fa-fw fa-inbox'},
    {name: '机器注册', cssIcon: 'fa fa-fw fa-registered'}
  ];
  getedCode = false;
  machineCode;
  registerCode = '';
  constructor(private message: MessageService,
              private formBuilder: FormBuilder,
              public progress: ProgressBarService,
              private machineService: MachineCodeService,
              private configService: DvbConfigService) {
    this.registeForm = this.formBuilder.group({
      code: ['', Validators.required]
    });
    this.registeForm.patchValue({code: ''});
  }

  ngOnInit() {
    this.configService.get(this.configService.urls.get_machine_code).subscribe((resp) => {
      this.machineCode = resp.body;
      this.message.sendMessage('page load progress', this);
    });
  }
  getMachineCode() {
    this.getedCode = true;
    this.configService.post(this.configService.urls.register, this.registeForm.value).subscribe((resp) => {
      alert('注册成功!');
    }, (error) => {
      alert('注册失败!');
    });
  }
  ngAfterViewInit(): void {
    this.message.sendMessage('breadcrumb changed', this, this.breadcrumb);
  }

  ngOnDestroy(): void {
  }

}
